<?php
class Model_Inventory extends Model_Template
{
	
	public function __construct()
	{
		parent::__construct();
		$this->className = 'Model_Table_Inventory';

	}
	
	
	////////////////////////////////////////////////////////////return all inventory items
	public function getAllInventoryItems()
	{
		//Select all inventory item query
		$sqlAllInventoryItem = 'SELECT * FROM inventory;';
		
		//Prepare statement
		$allInventoryItem = Controller_Template::$db->prepareEKADBPDO($sqlAllInventoryItem, $this->className, array('inventory_id','product_description','product_name','product_quantity'));
		
		//Execute statement
		$allInventoryItem->execute();
		
		
		return $allInventoryItem->fetchAll();
	}
	
	
	///////////////////////////////////////////////////////////create an inventory item
	public function createNewInventoryItem($newII)
	{
		//Create new inventory item query
		$sqlCreateInventoryItem = 'INSERT INTO inventory (product_description,product_name,product_quantity) VALUES (?,?,?);';
		
		//Prepare statement
		$createInventoryItem = Controller_Template::$db->prepareEKADBPDO($sqlCreateInventoryItem, $this->className, array('inventory_id','product_description','product_name','product_quantity'));
		
		//Execute statement
		$createInventoryItem->execute(array($newII[':product_description'],$newII[':product_name'],$newII[':product_quantity']));
		
		if($createInventoryItem->rowCount()==1)
		{
			return Controller_Template::$db->lastInsertId();
		}
	}
	
	/////////////////////////////////////////////////////////delete an inventory item
	public function deleteInventoryItem($targetItem)
	{
		//Delete inventory item query
		$sqlDeleteInventoryItem = 'DELETE FROM inventory WHERE inventory_id=?;';
		
		//Prepare statement
		$deleteInventoryItem = Controller_Template::$db->prepareEKADBPDO($sqlDeleteInventoryItem, $this->className, array('inventory_id','product_description','product_name','product_quantity'));
		
		//Execute statement
		$deleteInventoryItem->execute(array($targetItem));
		
		return $deleteInventoryItem->rowCount();
		
	}
	
	
	
	//////////////////////////////////////////////////////////update an inventory item
	public function updateInventoryItem($targetItem)
	{
		//Update inventory item query
		$sqlUpdateInventoryItem = 'UPDATE inventory SET product_description=?,product_name=?,product_quantity=? WHERE inventory_id =?;';
		
		//Prepare statement
		$updateInventoryItem = Controller_Template::$db->prepareEKADBPDO($sqlUpdateInventoryItem, $this->className, array('inventory_id','product_description','product_name','product_quantity'));
		
		//Execute statement
		$updateInventoryItem->execute(array($targetItem[':product_description'],$targetItem[':product_name'],$targetItem[':product_quantity'],$targetItem[':product_id']));
		
		return $updateInventoryItem->rowCount();
	}
	
}